<link href="<?php echo URL::base() ?>media/vendor/css/ui-lightness/jquery-ui-1.10.4.custom.css" rel="stylesheet">
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-1.7.1.min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.ui.core.min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.ui.widget.min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.ui.position.min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.ui.autocomplete.min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-1.7.1.min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-ui-1.10.4.custom.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-ui-timepicker-addon.min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-ui-timepicker-zh-CN.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.ui.datepicker-zh-TW.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/Validform_v5.3.2_ncr_min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/artdialog/jquery.artDialog.js"></script>
<script>

    /**
     * 显示供应商列表
     */
    function show_supplier_lists(supplier_data)
    {
        if(supplier_data.supplier_view)
        {
            $("#supplier_id").empty();
            $("#supplier_id").append(supplier_data.supplier_view);
        }
    }

    /**
     * 显示供应商详细信息
     */
    function show_supplier_info(supplier_info) {
        if (supplier_info.supplier_contanct) {
            /**
             * 修改替换联系人选择框
             */
            $("#supplier_contact").empty();
            $("#supplier_contact").append(supplier_info.supplier_contanct);
        }

        if (supplier_info.supplier_payway) {
            /**
             * 修改替换付款方式选择框
             */
            $("#select_payway").empty();
            $("#select_payway").append(supplier_info.supplier_payway);
        }


        if (supplier_info.supplier_paybank) {
            /**
             * 修改替换付款帐号选择框
             */
            $("#select_paybankinfo").empty();
            $("#select_paybankinfo").append(supplier_info.supplier_paybank);
        }

        if(supplier_info.coinid) {
            if(supplier_info.coinid != 1) {
                $(".rateTypeControl").removeClass("hidden");
            }
        }
    }

    function show_supplier_sku_info(supplier_sku_info)
    {
        var id = 1;
        if(supplier_sku_info)
        {
            if(supplier_sku_info.sku)
            {
                $("#"+id).children().eq(0).find("input").val(supplier_sku_info.sku);
            }
            if(supplier_sku_info.sku_name)
            {
                $("#"+id).children().eq(1).text(supplier_sku_info.sku_name);
            }
            if(supplier_sku_info.nuit)
            {
                $("#"+id).children().eq(2).text(supplier_sku_info.nuit);
            }
            if(supplier_sku_info.place)
            {
                $("#"+id).children().eq(3).text(supplier_sku_info.place);
            }
            if(supplier_sku_info.price)
            {
                $("#"+id).children().eq(5).find("input").val(supplier_sku_info.price);
            }
            if(supplier_sku_info.delivery)
            {
                $("#"+id).children().eq(9).find("input").val(supplier_sku_info.delivery);
            }
            if(supplier_sku_info.sku_price)
            {
                $("#"+id).children().eq(10).text(supplier_sku_info.sku_price);
            }
            if(supplier_sku_info.status)
            {
                $("#"+id).children().eq(11).text(supplier_sku_info.status);
            }
        }
    }

    /**
     * 获取供应商sku信息，以及sku信息
     */
    function get_supplier_sku_info(supplier_id,sku)
    {

        if(supplier_id
            && sku)
        {
            $.ajax(
                {
                    type: 'post',
                    url: '<?php echo url::base(true)?>purchase/getskuinfo',
                    dataType: 'json',
                    data: {"supplier_id": supplier_id,"sku":sku},
                    async: false,
                    success: function (re_data) {
                        if(re_data){
                            show_supplier_sku_info(re_data);
                        }
                    },
                    error: function () {
                    }
                }
            );
        }
    }
    /**
     * 根据供应商名称查询供应商的信息
     * @param supplier_id
     */
    function get_supplier_info(supplier_id) {
        if(supplier_id){
        $.ajax(
            {
                type: 'post',
                url: '<?php echo url::base(true)?>purchase/supplierpayway',
                dataType: 'json',
                data: {"supplier_id": supplier_id},
                async: false,
                success: function (re_data) {
                    if(re_data)
                    {
                      show_supplier_info(re_data);
                    }
                },
                error: function () {
                }
            }
        );
        }
    }

    /**
     * 通过sku查询供应商和sku信息
     */
    function select_sku_supplier(sku) {
        $.ajax(
            {
                type: 'post',
                url: '<?php echo url::base(true)?>purchase/get_sku_info',
                dataType: 'json',
                data: {"sku": sku},
                async: false,
                success: function (re_data) {
                    if(re_data){
                    /**
                     * 显示供应商列表
                     */
                    show_supplier_lists(re_data);
                    if(re_data.supplier_dafault_id)
                    {
                        get_supplier_info(re_data.supplier_dafault_id);
                        get_supplier_sku_info(re_data.supplier_dafault_id,sku);
                    }
                    }

                },
                error: function () {
                }
            }
        );
    }

    /**
     * 选择供应商事件处理
     */
    function select_supplier(obj)
    {
        var sku = $("#sku_input").val();
        var supplier_id = $(obj).val();
        get_supplier_info(supplier_id);
        get_supplier_sku_info(supplier_id,sku);
    }

    /**
     * 获取创建采购单的供应商信息
     */
    function get_purchase_info_supplier()
    {
        var supplier_id = $('#supplier_id').val();
        if(supplier_id == 0)
        {
//      alert('请选择供应商');return false;
        }

        var supplier_contact = $("#supplier_contact").val();
        if(supplier_contact == 0)
        {
//        alert('请选择供应商联系人');return false;
        }
        var select_payway = $("#select_payway").val();
        if(select_payway == 0)
        {
//        alert('请选择供应商付款方式');return false;
        }

        var select_paybankinfo = $("#select_paybankinfo").val();
        if(select_paybankinfo == 0)
        {
//        alert('请选择供应商付款帐号');return false;
        }
        /*
         * yuqing 检测仓库是否选定
         */
        var select_store = $("#select_store").val();
        if(select_store==-1){
            alert('请选择仓库');
            return false;
        }
        /*
         * yuqing
         *
         */

        var coinRate = 0;
        var rateType = 0;
        if($(".rateTypeControl").hasClass("hidden")) {
        }else{
            var rateType = $("#rateType").val();
            if(rateType == 0) {
                alert('请选择汇率类型');
                return false;
            }

            if(rateType == 40) {
                if($("#coinRate").val() != 0 && $("#coinRate").val()){
                    coinRate = $("#coinRate").val();
                }else{
                    alert('请填写汇率值');
                    return false;
                }
            }
        }

        var remark = $("#remark").val();
        var transportation_expense = $("#transportation_expense").val();

        //var coin = $("#coin").val();
        /**
         *  供应商信息
         *  供应商id
         *  付款方式id
         *  付款帐号id
         *  运费
         *  备注
         */
        var supplier_info = {"supplier_id":supplier_id,
            "contact_id":supplier_contact,
            "pay_way":select_payway,
            "payaccountinfo_id":select_paybankinfo,
            "transportation_expense":transportation_expense,
            "rateType":rateType,
            "coinRate": coinRate,
            "remark":remark,
            "createtype":1,
            'store':select_store

        };

        return supplier_info;
    }

    /**
     *  获取某一行的数据
     *
     */
    function get_purchase_sku_info_row(id)
    {
        if($("#"+id).children('td').find('input').attr('readonly'))
        {
            var sku_info= {
                "sku":$("#"+id).children('td').eq(0).find("input").val(),
                "sku_name":$("#"+id).children('td').eq(1).text(),
                "storage_position":$("#"+id).children('td').eq(3).text(),
                "sku_standard":"",
                "quantity":$("#"+id).children('td').eq(6).find('input').val(),
                "single_price":$("#"+id).children('td').eq(5).find('input').val(),
                "tax_rate":$("#"+id).children('td').eq(7).find('input').val(),
                "remark":"",
                "arrive_date":$("#"+id).children('td').eq(9).find('input').val(),
                "tax_way":$("#"+id).children('td').eq(4).find("select").val(),
                "sku_unit":$("#"+id).children('td').eq(1).text(),
                "prime_cost":$("#"+id).children('td').eq(10).text(),
                "supplier":$('#supplier_id').val()
            }

            return sku_info;
        }
        else
        {
            return false;
        }
    }


    /**
     * 获取采购单的SKU信息
     */
    function get_purchase_sku_info()
    {
        var sku_info_arr = [];
        $("#row").children('tr').each(function(){
            var sku_info = get_purchase_sku_info_row($(this).attr('id'));
            if(sku_info)
            {
                sku_info_arr.push(sku_info);
            }
        });

        if(sku_info_arr)
        {
            return sku_info_arr;
        }
        else
        {
            alert('请选择SKU');
            return false;
        }
    }

    /**
     * 获取计算金额的信息
     */
    function get_money_info(obj)
    {
        var id  =  $(obj).parent('td').parent('tr').attr('id');

        if(!$("#"+id).children('td').eq(0).find('input').attr('readonly'))
        {
            return false;
        }
        var money_info = {
            "quantity":$("#"+id).children('td').eq(6).find('input').val(),
            "single_price":$("#"+id).children('td').eq(5).find('input').val(),
            "tax_rate":$("#"+id).children('td').eq(7).find('input').val(),
            "tax_way":$("#"+id).children('td').eq(4).find("select").val()
        }

        return money_info;
    }

    function show_money(obj,money)
    {
        if(money){
            var id  =  $(obj).parent('td').parent('tr').attr('id');
            $("#"+id).children('td').eq(5).find('input').val(money.single_price);
            $("#"+id).children('td').eq(6).find('input').val(money.quantity);
            $("#"+id).children('td').eq(7).find('input').val(money.tax_rate);
            $("#"+id).children('td').eq(8).find('input').val(money.money);
        }
    }

    function check_single_price(obj)
    {
        var id  =  $(obj).parent('td').parent('tr').attr('id');

        if(!$("#"+id).children('td').eq(0).find('input').attr('readonly'))
        {
            return false;
        }
        var sku=$("#"+id).children('td').eq(0).find("input").val();
        var single_price=$("#"+id).children('td').eq(5).find('input').val();
        
        if(single_price){
            $.ajax(
                {
                    type: 'post',
                    url: '<?php echo url::base(true)?>purchase/check_single_price',
                    dataType: 'json',
                    data: {"single_price":single_price,'sku':sku},
                    async: false,
                    success: function (re_data) {
                        if(re_data.code==2)
                        {
                            alert('注意：单价高于基础资料系统单价');
                        }
                    },
                    error: function () {
                    }
                }
            );
        }
    }

    function check_sku_status(obj)
    {
        var id  =  $(obj).parent('td').parent('tr').attr('id');

        if(!$("#"+id).children('td').eq(0).find('input').attr('readonly'))
        {
            return false;
        }
        var sku=$("#"+id).children('td').eq(0).find("input").val();
        $.ajax(
            {
                type: 'post',
                url: '<?php echo url::base(true)?>purchase/check_sku_status',
                dataType: 'json',
                data: {'sku':sku},
                async: false,
                success: function (re_data) {
                    if(re_data.code==2)
                    {
                        alert(re_data.mes);
                    }
                },
                error: function () {
                    alert('系统错误');
                }
            }
        );
    }

    /**
     * 计算价格与基础资料价格进行比较，如果大于，则不允许下单
     */
    function count_money_and_price(obj)
    {
        check_single_price(obj);
        var money_info = get_money_info(obj);
        if(money_info){
            $.ajax(
                {
                    type: 'post',
                    url: '<?php echo url::base(true)?>purchase/count_money',
                    dataType: 'json',
                    data: {"money_info":money_info},
                    async: false,
                    success: function (re_data) {
                        if(re_data)
                        {
                            show_money(obj,re_data);
                        }
                    },
                    error: function () {
                    }
                }
            );
        }
    }
    //输入数量后提示
    function count_money_in_count(obj)
    {
        var td_single_price = $(obj).parent().prev().find("input");
        check_sku_status(td_single_price);

        check_single_price(td_single_price);
        var money_info = get_money_info(obj);
        if(money_info){
            $.ajax(
                {
                    type: 'post',
                    url: '<?php echo url::base(true)?>purchase/count_money',
                    dataType: 'json',
                    data: {"money_info":money_info},
                    async: false,
                    success: function (re_data) {
                        if(re_data)
                        {
                            show_money(obj,re_data);
                        }
                    },
                    error: function () {
                    }
                }
            );
        }
        //num_blur();
    }


    /**
     * 重新计算金额
     */
    function count_money(obj)
    {

        var money_info = get_money_info(obj);
        if(money_info){
            $.ajax(
                {
                    type: 'post',
                    url: '<?php echo url::base(true)?>purchase/count_money',
                    dataType: 'json',
                    data: {"money_info":money_info},
                    async: false,
                    success: function (re_data) {
                        if(re_data)
                        {
                            show_money(obj,re_data);
                        }
                    },
                    error: function () {
                    }
                }
            );
        }
    }

    function change_txt(money, coin) {
        return '财务需折算为' + coin +'付款，汇率以付款当天汇率为准。';
    }

    //修改运费后备注自动加上
    function text_money_change(obj) {
        if($(".all_m").length) {
            var coin = $("#coin").val();
            if(parseInt(coin) > 1) {
                var f = 0;
                $(".all_m").each(function(){
                    if(parseFloat($(this).children("input").eq(0).val())) {
                        f += parseFloat($(this).children("input").eq(0).val());
                    }
                });
                if(f) {
                    var coin_name = $("#coin").children("option[value='" + coin +"']").text();
                    f += parseFloat($(obj).val());
                    $("#remark").val(change_txt(f, coin_name));
                }
            }
        }
    }

    function coin_change(obj) {
        //判断是否有有效的SKU行
        if($(obj).val()<=1) {
            var reg=/^.*?当天汇率.*?$/;
            if(reg.test($("#remark").val())) {
                if(window.confirm("确定修改为非外币吗，修改后备注将清空？")) {
                    $("#remark").val("");
                }
            }
            return;
        }

        if($(".all_m").length) {
            var f = 0;
            $(".all_m").each(function(){
                if(parseFloat($(this).children("input").eq(0).val())) {
                    f += parseFloat($(this).children("input").eq(0).val());
                }
            });
            if(f) {
                var coin = $(obj).val();
                var coin_name = $(obj).children("option[value='" + coin +"']").text();
                f += parseFloat($("#transportation_expense").val());
                $("#remark").val(change_txt(f, coin_name));
            }
        }
    }

    function num_blur() {
        //有数量的时候判断币种
        if($(".all_m").length) {
            var coin = $("#coin").val();
            if(parseInt(coin) > 1) {
                var f = 0;
                $(".all_m").each(function(){
                    if(parseFloat($(this).children("input").eq(0).val())) {
                        f += parseFloat($(this).children("input").eq(0).val());
                    }
                });
                if(f) {
                    var coin_name = $("#coin").children("option[value='" + coin +"']").text();
                    f += parseFloat($("#transportation_expense").val());
                    $("#remark").val(change_txt(f, coin_name));
                }
            }
        }
    }

    function coinOrNot() {
        if($("#coin").val()==0) {
            alert("请选择付款币种");
            return false;
        }
        return true;
    }
    $(document).ready(function () {

        /**
         * 设置时间控件
         */
        $.datepicker.setDefaults({ dateFormat: 'yy-mm-dd' });

        $(document).on('focus', ".datepicker", function (e) {
            $(this).datepicker({
                minDate: new Date()
            });
        });

        /**
         *  输入sku自动提示功能
         */
        $("#sku_input").autocomplete({
            autoFocus: true,
            minLength: 0,
            autoFill: true,
            source: "<?php echo URL::base(true)?>purchase/get_sku_like",
            select: function (event, ui) {
                /**
                 * 从建议列表中选中之后触发的事件
                 */
                $("#sku_input").val(ui.item.label);
                $("#sku_input").attr("readonly", "readonly");
                select_sku_supplier(ui.item.label);
                return false;
            }
        });

        $("#tijiao").click(function(){
            var supplier_info = get_purchase_info_supplier();
            var sku_info = get_purchase_sku_info();
            //if(!coinOrNot())return false;

            if(supplier_info && sku_info)
            {
                $.ajax(
                    {
                        type: 'post',
                        url: '<?php echo url::base(true)?>purchase/createpurchasenew',
                        dataType: 'json',
                        data: {"purchaseorders": supplier_info,"purchaseorder_details":sku_info},
                        async: false,
                        success: function (re_data) {
                            if(re_data.code == 2)
                            {
                                var tips = '创建成功，采购单号'+re_data.id;
                                alert(tips);
                                window.location.reload();
                            }
                        },
                        error: function () {
                        }
                    }
                );
            }
        });

        $("#select_paybankinfo").change(function(){
            if($(".rateTypeControl").hasClass("hidden")){
                $(".rateTypeControl").removeClass("hidden");
            }else{
                $(".rateTypeControl").addClass("hidden");
            }
        });

        $("#rateType").change(function(){
            if($(this).val() == 40){
                $(".rateControl").removeClass('hidden');
            }else{
                $(".rateControl").addClass('hidden');
            }
        });


    });
</script>
    <div class="form-horizontal">
        <div class="control-group">
            <label class="control-label" for="sku_input">计划员*</label>

            <div class="controls">
                <input  type="text" class="input-xlarge" id="sku_input" name="sku" placeholder="请输入SKU"
                       >
                <button onclick="window.location.reload()">重选</button>
            </div>
        </div>

        <div class="control-group">
            <label class="control-label" for="supplier_name">供应商*</label>

            <div class="controls" id="group">
                <input  type="text" class="input-xlarge" name="supplier_name" id="supplier_id"  placeholder="宁波市鄞州佳盛水泵电机厂"
                >
                <button onclick="window.location.reload()">重置</button>
            </div>
        </div>
        <div class="control-group" id="select_supplier_contact_div">
            <label class="control-label" for="supplier_contact">联系人:</label>

            <div class="controls">
                <select class="input-xlarge" name="supplier_contact" id="supplier_contact" style="width: 284px">
                    <option value="0">请选择联系人</option>
                </select>
            </div>
        </div>
        <div class="control-group" id="supplier_payway_div">
            <label class="control-label" for="input01">付款方式:</label>

            <div class="controls">
                <select class="input-xlarge" name="paytype" id="select_payway" style="width: 284px">
                    <option value="0">请选择付款方式</option>
                </select>
            </div>
        </div>
        <div class="control-group" id="select_paybankinfo_div">
            <label class="control-label" for="select_paybankinfo">付款帐号:</label>

            <div class="controls">
                <select class="input-xlarge" name="select_paybankinfo" id="select_paybankinfo" style="width: 284px">
                     <option value="0">请选择付款帐号</option>
                </select>
            </div>
        </div>

        <div class="control-group">
            <label class="control-label">主体:</label>

            <div class="controls">
                <select class="input-xlarge" name="select_yks"  style="width: 284px">
                    <option value="-1">--请选择--</option>
                    <option value="0">--深圳YKS--</option>
                    <option value="0">--苏州YKS--</option>
                </select>
            </div>
        </div>
        <!-- 仓库选择----------yuqing 2015-08-15--------------------------------->

        <div class="control-group">
            <label class="control-label" for="select_paybankinfo">仓库:</label>
            <div class="controls">
                <select class="input-xlarge" name="select_store" id="select_store" style="width: 284px">
                    <option value="-1">--请选择--</option>
                    <option value="0">--国内仓--</option>
                    <?php if(!empty($stors)){
                        foreach($stors as $stor_id=>$stor){?>
                            <option value="<?php echo $stor_id;?>">--<?php echo $stor;?>--</option>
                        <?php }}?>
                </select>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">运输方式:</label>

            <div class="controls">
                <select class="input-xlarge" name="select_yks"  style="width: 284px">
                    <option value="-1">--请选择--</option>
                    <option value="0">--海运--</option>
                    <option value="0">--空运--</option>
                </select>
            </div>
        </div>

        <div class="control-group">

            <!-- Textarea -->
            <label class="control-label">备注:</label>

            <div class="controls">
                <textarea type="" class="input-xlarge" name="remark"
                          id="remark"> </textarea>
            </div>
        </div>
        <div class="control-group" style="display:none">
            <label class="control-label" for="input01"></label>

            <div class="controls">
                <input type="text" class="input-xlarge" name="supplier" id="supplier" value="" autocomplete="off"/>
            </div>
        </div>
    </div>
    <table class="table table1 table-hover table-striped table-condensed">
        <thead>
        <tr style="  background-color:#4EA392; color:#fff;">

            <th>
                SKU
            </th>
            <th>
                中文名称
            </th>
            <th>报关品名</th>
            <th>商品编码</th>
            <th>
                单位
            </th>
            <th>
                储位
            </th>

            <th>
                单价<span style="color: red">￥</span>
            </th>
            <th>
                数量
            </th>
            <th>
                总金额
            </th>
            <th>
                交货日期
            </th>
            <th>
                主体
            </th>
            <th>仓库</th>
            <th>
                状态
            </th>
            <th>操作</th>


        </tr>
        </thead>
        <tbody id="row">
        <tr id="1">

            <td>
             ZM537500
            </td>
            <td>
                不计税sku
            </td>
            <td>

            </td>
            <td>

            </td>

            <td> </td>
            <td>

            </td>
            <td>

            </td>
            <td>
               </td>
            <td class="all_m">
                </td>
            <td>


            </td>
            <td>

            </td>
            <td>

            </td>

            <td></td>
            <td><div class="dropdown">
                    <button type="button" class="btn dropdown-toggle" id="dropdownMenu1"
                            data-toggle="dropdown">
                        操作
                        <span class="caret"></span>
                    </button>
                    <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
                        <li role="presentation">
                            <a role="menuitem" tabindex="-1" href="#" >新增</a>
                        </li>
                        <li role="presentation">
                            <a role="menuitem" tabindex="-1" href="#">删除</a>
                        </li>

                    </ul>
                </div></td>
        </tr>

        </tbody>

    </table>
    <button id="tijiao" class="btn btn-info" style="margin-left:10px;">一键创建</button>
<?php ?>
<a></a>
<?php ?>